﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebUI.Controls
{
    public partial class GridFilterPaging : BaseControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public event EventHandler NeedListControl;
        public virtual void OnNeedListControl()
        {
        }

        private BaseListControl listControl;

        public BaseListControl ListControl
        {
            get 
            {
                NeedListControl(this, null);
                return listControl; 
            }
            set { listControl = value; }
        }

        string filteredField;

        public string FilteredField
        {
            get { return filteredField; }
            set { filteredField = value; }
        }

        //Sort by Alphabet
        public String SelectedSortButtonID
        {
            get { return ViewState[ClientID + "_SelectedSortButtonID"] != null ? ViewState[ClientID + "_SelectedSortButtonID"].ToString() : ""; }
            set { ViewState[ClientID + "_SelectedSortButtonID"] = value; }
        }

        public String DefaultFilter
        {
            get { return ViewState[ClientID + "_DefaultFilter"] != null ? ViewState[ClientID + "_DefaultFilter"].ToString() : ""; }
            set { ViewState[ClientID + "_DefaultFilter"] = value; }
        }

        public void Reset()
        {
            if (!String.IsNullOrEmpty(SelectedSortButtonID))
            {
                Button currentBtn = FindControl(SelectedSortButtonID) as Button;
                if (currentBtn != null)
                    currentBtn.Attributes["class"] = "sortButton";
            }
            SelectedSortButtonID = "";
            ListControl.CurrentPageIndex = 0;
            ListControl.Filter = DefaultFilter;
        }

        public void btnAlphabet_OnClick(Object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            btn.Attributes["class"] = "sortButtonSelected";
            if (!String.IsNullOrEmpty(SelectedSortButtonID))
            {
                Button currentBtn = FindControl(SelectedSortButtonID) as Button;
                if (currentBtn != null)
                    currentBtn.Attributes["class"] = "sortButton";
            }

            if (btn.ID != SelectedSortButtonID)
            {
                String ch = btn.ID.Replace("btn", "");
                ListControl.CurrentPageIndex = 0;
                ListControl.SelectedValue = "";
                if(DefaultFilter != "")
                    ListControl.Filter = DefaultFilter + " and (" + filteredField + " like '" + ch + "%')";
                else
                    ListControl.Filter = filteredField + " like '" + ch + "%'";
                SelectedSortButtonID = btn.ID;
            }
            else
            {
                ListControl.CurrentPageIndex = 0;
                ListControl.Filter = DefaultFilter;
                ListControl.SelectedValue = "";
                SelectedSortButtonID = "";
            }

            ListControl.Refresh();
        }

        //Change Page Size
        public void ddlPageSize_OnSelectedIndexChanged(Object sender, EventArgs e)
        {
            ListControl.CurrentPageIndex = 0;
            ListControl.PageSize = int.Parse(ddlPageSize.SelectedValue);
            ListControl.Refresh();
        }
    }
}